using App.Model;
using System;
using System.Windows.Forms;

namespace App.UI
{
    internal class CheckBox: CheckedListBox
    {
        public event EventHandler<RecordEvent> RecordItemCheck;

        public class RecordEvent
        {
            public int Index { set; get; }
            public bool State { set; get; }

            public RecordEvent(int index, bool state) {
                this.Index = index;
                this.State = state;
            }
        }

        public void setData(Record[] records)
        {
            this.Items.Clear();
            foreach (Record record in records) 
            {
                this.Items.Add(record.Label);
            }
            this.ItemCheck += HandleItemCheck;
         }

        private void HandleItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue != CheckState.Indeterminate)
            {
                bool state = e.NewValue != CheckState.Checked;
                RecordItemCheck?.Invoke(this, new RecordEvent(e.Index, state));
            }
        }

    }
}
